#!/bin/sh

error_exit() {
  echo "$1" 1>&2
  exit 1
}

if  ! which spatialite >/dev/null; then
    echo "spatialite not found which is required.  Please install via:  sudo apt-get install spatialite-bin"
    exit 1
fi

if  ! which unzip >/dev/null; then
    echo "unzip not found which is required.  Please install via:  sudo apt-get install unzip"
    exit 1
fi

rm -rf dist
rm -f ./timezones-with-oceans.shapefile.zip

url="https://github.com/evansiroky/timezone-boundary-builder/releases/download/2018d/timezones-with-oceans.shapefile.zip"

cat << EOF > alias_tz.csv
Pacific/Ponape,Pacific/Pohnpei
Pacific/Midway,Pacific/Pago_Pago
Pacific/Samoa,Pacific/Pago_Pago
US/Samoa,Pacific/Pago_Pago
Kwajalein,Pacific/Kwajalein
Pacific/Johnston,Pacific/Honolulu
US/Hawaii,Pacific/Honolulu
Pacific/Saipan,Pacific/Guam
Chile/EasterIsland,Pacific/Easter
Pacific/Truk,Pacific/Chuuk
Pacific/Yap,Pacific/Chuuk
NZ-CHAT,Pacific/Chatham
Antarctica/McMurdo,Pacific/Auckland
Antarctica/South_Pole,Pacific/Auckland
NZ,Pacific/Auckland
Europe/Busingen,Europe/Zurich
Europe/Vaduz,Europe/Zurich
Poland,Europe/Warsaw
Europe/San_Marino,Europe/Rome
Europe/Vatican,Europe/Rome
Europe/Bratislava,Europe/Prague
Arctic/Longyearbyen,Europe/Oslo
Atlantic/Jan_Mayen,Europe/Oslo
W-SU,Europe/Moscow
Europe/Belfast,Europe/London
Europe/Guernsey,Europe/London
Europe/Isle_of_Man,Europe/London
Europe/Jersey,Europe/London
GB,Europe/London
GB-Eire,Europe/London
Portugal,Europe/Lisbon
Asia/Istanbul,Europe/Istanbul
Turkey,Europe/Istanbul
Europe/Mariehamn,Europe/Helsinki
Eire,Europe/Dublin
Europe/Tiraspol,Europe/Chisinau
Europe/Ljubljana,Europe/Belgrade
Europe/Podgorica,Europe/Belgrade
Europe/Sarajevo,Europe/Belgrade
Europe/Skopje,Europe/Belgrade
Europe/Zagreb,Europe/Belgrade
UTC,Etc/UTC
Etc/Universal,Etc/UTC
Etc/Zulu,Etc/UTC
Universal,Etc/UTC
Zulu,Etc/UTC
UCT,Etc/UCT
Etc/GMT+0,Etc/GMT
Etc/GMT0,Etc/GMT
Etc/GMT-0,Etc/GMT
GMT,Etc/GMT
Etc/Greenwich,Etc/GMT
GMT+0,Etc/GMT
GMT0,Etc/GMT
GMT−0,Etc/GMT
Greenwich,Etc/GMT
Australia/Canberra,Australia/Sydney
Australia/ACT,Australia/Sydney
Australia/NSW,Australia/Sydney
Australia/West,Australia/Perth
Australia/Victoria,Australia/Melbourne
Australia/LHI,Australia/Lord_Howe
Australia/Tasmania,Australia/Hobart
Australia/North,Australia/Darwin
Australia/Yancowinna,Australia/Broken_Hill
Australia/Queensland,Australia/Brisbane
Australia/South,Australia/Adelaide
Iceland,Atlantic/Reykjavik
Atlantic/Faeroe,Atlantic/Faroe
Asia/Rangoon,Asia/Yangon
Asia/Kashgar,Asia/Urumqi
Asia/Ulan_Bator,Asia/Ulaanbaatar
Japan,Asia/Tokyo
Asia/Thimbu,Asia/Thimphu
Iran,Asia/Tehran
ROC,Asia/Taipei
Singapore,Asia/Singapore
Asia/Chongqing,Asia/Shanghai
Asia/Chungking,Asia/Shanghai
Asia/Harbin,Asia/Shanghai
PRC,Asia/Shanghai
ROK,Asia/Seoul
Asia/Aden,Asia/Riyadh
Asia/Kuwait,Asia/Riyadh
Asia/Bahrain,Asia/Qatar
Asia/Ujung_Pandang,Asia/Makassar
Asia/Macao,Asia/Macau
Asia/Calcutta,Asia/Kolkata
Asia/Katmandu,Asia/Kathmandu
Asia/Tel_Aviv,Asia/Jerusalem
Israel,Asia/Jerusalem
Hongkong,Asia/Hong_Kong
Asia/Saigon,Asia/Ho_Chi_Minh
Asia/Muscat,Asia/Dubai
Asia/Dacca,Asia/Dhaka
Asia/Phnom_Penh,Asia/Bangkok
Asia/Vientiane,Asia/Bangkok
Asia/Ashkhabad,Asia/Ashgabat
Canada/Central,America/Winnipeg
Canada/Yukon,America/Whitehorse
Canada/Pacific,America/Vancouver
America/Montreal,America/Toronto
Canada/Eastern,America/Toronto
America/Ensenada,America/Tijuana
America/Santa_Isabel,America/Tijuana
Mexico/BajaNorte,America/Tijuana
Canada/Newfoundland,America/St_Johns
Brazil/East,America/Sao_Paulo
Chile/Continental,America/Santiago
America/Porto_Acre,America/Rio_Branco
Brazil/Acre,America/Rio_Branco
Canada/Saskatchewan,America/Regina
America/Anguilla,America/Port_of_Spain
America/Antigua,America/Port_of_Spain
America/Dominica,America/Port_of_Spain
America/Grenada,America/Port_of_Spain
America/Guadeloupe,America/Port_of_Spain
America/Marigot,America/Port_of_Spain
America/Montserrat,America/Port_of_Spain
America/St_Barthelemy,America/Port_of_Spain
America/St_Kitts,America/Port_of_Spain
America/St_Lucia,America/Port_of_Spain
America/St_Thomas,America/Port_of_Spain
America/St_Vincent,America/Port_of_Spain
America/Tortola,America/Port_of_Spain
America/Virgin,America/Port_of_Spain
US/Arizona,America/Phoenix
America/Cayman,America/Panama
Brazil/DeNoronha,America/Noronha
US/Eastern,America/New_York
Mexico/General,America/Mexico_City
Mexico/BajaSur,America/Mazatlan
Brazil/West,America/Manaus
US/Pacific,America/Los_Angeles
US/Pacific-New,America/Los_Angeles
America/Louisville,America/Kentucky/Louisville
Jamaica,America/Jamaica
America/Knox_IN,America/Indiana/Knox
US/Indiana-Starke,America/Indiana/Knox
America/Fort_Wayne,America/Indiana/Indianapolis
America/Indianapolis,America/Indiana/Indianapolis
US/East-Indiana,America/Indiana/Indianapolis
Cuba,America/Havana
Canada/Atlantic,America/Halifax
Canada/Mountain,America/Edmonton
US/Michigan,America/Detroit
America/Shiprock,America/Denver
Navajo,America/Denver
US/Mountain,America/Denver
America/Aruba,America/Curacao
America/Kralendijk,America/Curacao
America/Lower_Princes,America/Curacao
US/Central,America/Chicago
America/Coral_Harbour,America/Atikokan
America/Mendoza,America/Argentina/Mendoza
America/Jujuy,America/Argentina/Jujuy
America/Cordoba,America/Argentina/Cordoba
America/Rosario,America/Argentina/Cordoba
America/Argentina/ComodRivadavia,America/Argentina/Catamarca
America/Catamarca,America/Argentina/Catamarca
America/Buenos_Aires,America/Argentina/Buenos_Aires
US/Alaska,America/Anchorage
America/Atka,America/Adak
US/Aleutian,America/Adak
Libya,Africa/Tripoli
Africa/Addis_Ababa,Africa/Nairobi
Africa/Asmara,Africa/Nairobi
Africa/Dar_es_Salaam,Africa/Nairobi
Africa/Djibouti,Africa/Nairobi
Africa/Kampala,Africa/Nairobi
Africa/Mogadishu,Africa/Nairobi
Indian/Antananarivo,Africa/Nairobi
Indian/Comoro,Africa/Nairobi
Indian/Mayotte,Africa/Nairobi
Africa/Blantyre,Africa/Maputo
Africa/Bujumbura,Africa/Maputo
Africa/Gaborone,Africa/Maputo
Africa/Harare,Africa/Maputo
Africa/Kigali,Africa/Maputo
Africa/Lubumbashi,Africa/Maputo
Africa/Lusaka,Africa/Maputo
Africa/Bangui,Africa/Lagos
Africa/Brazzaville,Africa/Lagos
Africa/Douala,Africa/Lagos
Africa/Kinshasa,Africa/Lagos
Africa/Libreville,Africa/Lagos
Africa/Luanda,Africa/Lagos
Africa/Malabo,Africa/Lagos
Africa/Niamey,Africa/Lagos
Africa/Porto-Novo,Africa/Lagos
Africa/Sao_Tome,Africa/Lagos
Africa/Maseru,Africa/Johannesburg
Africa/Mbabane,Africa/Johannesburg
Egypt,Africa/Cairo
Africa/Bamako,Africa/Abidjan
Africa/Banjul,Africa/Abidjan
Africa/Conakry,Africa/Abidjan
Africa/Dakar,Africa/Abidjan
Africa/Freetown,Africa/Abidjan
Africa/Lome,Africa/Abidjan
Africa/Nouakchott,Africa/Abidjan
Africa/Ouagadougou,Africa/Abidjan
Africa/Timbuktu,Africa/Abidjan
Atlantic/St_Helena,Africa/Abidjan
EOF

echo "downloading timezone polygon file." 1>&2 
curl -L -s -o ./timezones-with-oceans.shapefile.zip ${url} || error_exit "wget failed for " ${url}
unzip ./timezones-with-oceans.shapefile.zip 1>&2 || error_exit "unzip failed"

tz_file=$(mktemp)
spatialite_tool -i -shp ./dist/combined-shapefile-with-oceans -d ${tz_file} -t tz_world -s 4326 -g geom -c UTF-8 1>&2 || error_exit "spatialite_tool import failed"
spatialite ${tz_file} "SELECT CreateSpatialIndex('tz_world', 'geom');" 1>&2 || error_exit "SpatialIndex failed" 

spatialite ${tz_file} "DROP TABLE IF EXISTS "alias";" 1>&2 || error_exit "drop table alias failed"
spatialite ${tz_file} "CREATE TABLE "alias" (alias_TZID text,alias_new_TZID text);" 1>&2 || error_exit "create table alias failed"
spatialite -csv ${tz_file} ".import alias_tz.csv alias" 1>&2 || error_exit "alias table import failed"
spatialite ${tz_file} "update tz_world set TZID = (select alias_new_TZID from alias where TZID = alias_TZID) where TZID in (select alias_TZID from alias);" 1>&2 || error_exit "updating tz_world failed"
spatialite ${tz_file} "VACUUM;" 1>&2 || error_exit "VACUUM failed"
spatialite ${tz_file} "ANALYZE;" 1>&2 || error_exit "ANALYZE failed"

rm -rf dist
rm -f ./timezones-with-oceans.shapefile.zip alias_tz.csv
cat ${tz_file}
